package com.whmskj.xjlhbc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.domain.Bcry12zbgghgl;
import com.whmskj.xjlhbc.domain.PersonnelManagementDataSheets;
import com.whmskj.xjlhbc.mapper.Bcry12zbgghglMapper;
import com.whmskj.xjlhbc.service.IBcry12zbgghglService;
import com.whmskj.xjlhbc.service.IPersonnelManagementDataSheetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 织布工工号管理表Service实现类
 */
@Service
public class Bcry12zbgghglServiceImpl extends ServiceImpl<Bcry12zbgghglMapper, Bcry12zbgghgl> implements IBcry12zbgghglService {
    
    @Autowired
    private Bcry12zbgghglMapper bcry12zbgghglMapper;
    
    @Autowired
    private IPersonnelManagementDataSheetsService personnelManagementDataSheetsService;
    
    @Override
    public List<Bcry12zbgghgl> getWeaverListByClass(String ben) {
        return bcry12zbgghglMapper.selectByBen(ben);
    }
    

    
    @Override
    public List<PersonnelManagementDataSheets> getPersonnelByClass(String classType) {
        // 只接受固定值：织布甲班 / 织布乙班
        if (!"织布甲班".equals(classType) && !"织布乙班".equals(classType)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<PersonnelManagementDataSheets> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PersonnelManagementDataSheets::getClass22, classType)
                   .eq(PersonnelManagementDataSheets::getActiveOrNot, "在职");
        return personnelManagementDataSheetsService.list(queryWrapper);
    }
    
    @Override
    public List<String> getPersonnelNamesByClass(String classType) {
        List<PersonnelManagementDataSheets> list = getPersonnelByClass(classType);
        List<String> names = new ArrayList<>();
        for (PersonnelManagementDataSheets p : list) {
            // 仅使用姓名字段 fe1e（数据库存在该列）
            if (p.getFe1e() != null && !p.getFe1e().isEmpty()) {
                names.add(p.getFe1e());
            }
        }
        return names;
    }
    
    @Override
    public AjaxResult updateDriverNameById(Long id, String nameyee) {
        try {
            // 根据ID查询记录
            Bcry12zbgghgl weaver = getById(id);
            
            if (weaver == null) {
                return AjaxResult.error("未找到该记录");
            }
            
            // 只更新姓名字段
            weaver.setNameyee(nameyee);
            weaver.setUpdateTime(new Date());
            
            boolean result = updateById(weaver);
            if (result) {
                return AjaxResult.success("姓名更新成功");
            } else {
                return AjaxResult.error("姓名更新失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("更新失败：" + e.getMessage());
        }
    }
}
